#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
PointSize = 10
class BoxItem(QGraphicsItem):

    def __init__(self, position, scene, scls, color=Qt.blue, matrix=QMatrix()):
        super(BoxItem, self).__init__()

        self.rect = QRectF(0, 0, scls[0], scls[1])
        self.style = Qt.SolidLine
        self.color = color
        self.setPos(position)
        self.setMatrix(matrix)
        scene.clearSelection()
        scene.addItem(self)
        self.setSelected(True)
        self.setFocus()


    def parentWidget(self):
        return self.scene().views()[0]


    def boundingRect(self):
        return self.rect.adjusted(-2, -2, 2, 2)


    def paint(self, painter, option, widget):
        painter.setPen(self.color)
        painter.setBrush(self.color )
        
        painter.drawRect(self.rect)


class CritPointsItem(QGraphicsItem):

    def __init__(self, points, scls, matrix=QMatrix()):
        super(CritPointsItem, self).__init__()
        self.points = points

        self.scls = scls
        self.setMatrix(matrix)
        #scene.clearSelection()
        #scene.addItem(self)
        self.setSelected(True)
        self.setFocus()
    def computeBoundingBox(self):
        try:
            mx = self.points[0][0].x()
            my = self.points[0][0].y()
            for point in self.points[1:]:
                mx = max(mx,point[0].x())
                my = max(my,point[0].y())
            bb = QRectF(-2,-2,mx+self.scls[0],my+self.scls[1])
            return bb
        except Exception, error:
            #print "failed in computeBoundingBox", error
            return QRectF(-2,-2,0,0)
    def boundingRect(self):
        return self.computeBoundingBox()
    def parentWidget(self):
        return self.scene().views()[0]

    def paint(self, painter, option, widget):
        counter = 0
        for point in self.points:
            painter.setPen(point[1])
            painter.setBrush(point[1] )
            crd = point[0]
            painter.drawRect(QRectF(crd.x(),crd.y(),self.scls[0],self.scls[1]))
            counter += 1
        #print "drew %d points"%counter



